App({
  unitid: 'wisnuo',
  serverUrl: "http://192.168.1.171:8081",
  currentUrl: '',
  userInfo: null,
  userDetailInfo: null,
  appid: "wx7e9152e5955c5621",
  header: {
    'X-Requested-With': 'XMLHttpRequest',
    'token': '',
    'id': ''
  },
  globalData: {
    userInfo: null,
    navHeight: 0,
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight']
  },
  trim: function(str) {
    if (str) {
      return str.replace(/(^\s*)|(\s*$)/g, '');
    }
  },
  // 防止注入攻击
  checkInput: function(v) {
    var temp = v;
    v = v.replace(/\\|\/|\.|\'|\"|\<|\>/g, function(str) {
      return '';
    });
    if (v) {
      v = v.replace(/(^\s*)|(\s*$)/g, '');
    }
    var ret = v.length === temp.length ? false : true;

    if (!ret) {
      wx.showToast({
        title: '输入非法字符！',
        icon: 'none',
        duration: 3000
      })
    }
    return ret;
  },
  //匹配查询结果中的查询字符
  matchWord: function(word, str) {
    if (!str) {
      return {
        activeStr: '',
        str: ''
      };
    }
    var activeStr = '',
      arrStr = str.split(''),
      strIndex = str.indexOf(word),
      strLength = word.length;
    if (strIndex == -1) {
      return {
        activeStr: '',
        str: str
      };
    } else {
      activeStr = str.substr(strIndex, strLength);
      arrStr.splice(strIndex, strLength);
      str = arrStr.join('');
      return {
        activeStr: activeStr || '',
        str: str || ''
      };
    }
  },
  nodata: function(num) {
    var msg = '没有查询到匹配的信息';
    if (num > 0) {
      msg = '已是最后一页'
    }
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 3000
    });
  },
  //显示消失提示框
  showMsg: function(res) {
    wx.showToast({
      title: res.data.msg,
      icon: 'none',
      duration: 3000
    });
  },
  //弹框提示
  doFail: function(res) {
    wx.showToast({
      title: "系统内部异常:" + res.data.msg,
      icon: 'none',
      duration: 3000
    });
  },
  userLogin: function(username, password, backUrl) {
    var _this = this;
    //从内存中取数据
    if (username == undefined || username.length == 0) {
      username = wx.getStorageSync('acmc-u');
    }
    if (password == undefined || password.length == 0) {
      password = wx.getStorageSync('acmc-p');
    }
    if (username == undefined || username.length == 0 || password == undefined || password.length == 0) {
      //如果从登录页面请求
      if (backUrl && backUrl.indexOf('sys/login') != -1) {
        wx.showToast({
          title: '请输入用户名和密码',
          icon: 'none',
          duration: 3000
        });
      } else {
        wx.redirectTo({
          url: '/pages/sys/login'
        })
      }
    } else {
      wx.showLoading({
        title: '校验中,请等待……'
      });
      wx.request({
        url: _this.serverUrl + '/login',
        method: "POST",
        data: {
          username: username,
          password: password,
          unitid: _this.unitid
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          var status = res.data.status;
          wx.hideLoading();
          if (status == 200) {
            //将用户信息写入缓存
            var datas = res.data.data;
            _this.userInfo = datas;
            _this.header.token = datas.userToken;
            _this.header.id = datas.id;
            wx.setStorageSync('acmc-u', username);
            wx.setStorageSync('acmc-p', password);
            //页面跳转
            if (!backUrl && backUrl.indexOf('sys/login') != -1) {
              wx.navigateTo({
                url: backUrl
              });
            } else {
              if (_this.currentUrl) {
                wx.redirectTo({
                  url: _this.currentUrl
                })
              } else {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          } else if (status == 500) {
            _this.userInfo = null;
            _this.header.token = '';
            _this.header.id = '';
            wx.removeStorageSync('acmc-u');
            wx.removeStorageSync('acmc-p');
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 3000
            });
          }
        },
        complete: function() {
          wx.hideLoading();
        }
      })
    }
  },
  checkUser: function(url) {
    if (!this.userInfo || !this.header.token || !this.header.id) {
      this.currentUrl = url;
      this.userLogin('', '', url);
    }
  },
  getSystemInfo: function() {
    let t = this;
    wx.getSystemInfo({
      success: function(res) {
        t.globalData.systemInfo = res;
      }
    });
  },
  onLaunch: function() {
    wx.hideTabBar();
    //获取设备信息
    this.getSystemInfo();
    wx.getSystemInfo({
      success: res => {
        //导航高度
        this.globalData.navHeight = res.statusBarHeight;
      },
      fail(err) {
        console.log(err);
      }
    })

  },
  onShow: function() {
    wx.hideTabBar({
      success: function() {
        setTimeout(function() {
          wx.hideTabBar()
        }, 1)
      }
    });
  },
  onLoad: function() {
    wx.hideTabBar({
      success: function() {
        setTimeout(function() {
          wx.hideTabBar()
        }, 1)
      }
    });
  },
  onReady: function() {},
  bindchange: function(e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  currentBind: function(e) {
    const that = this;
    if (that.data.currentData === e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.currentTarget.dataset.current
      })
    }
  },
  globalData: {
    systemInfo: null, //客户端设备信息
    userInfo: null,
    tabBar: {
      selected: false,
      "backgroundColor": "#ffffff",
      "color": "#979795",
      "selectedColor": "#f6a91c",
      "list": [{
          "pagePath": "/pages/index/index",
          "iconPath": "/static/icon/tab-1.png",
          "selectedIconPath": "/static/icon/tab-2.png",
          "text": "首页"
        },
        {
          "pagePath": "/pages/apply/apply",
          "iconPath": "/static/icon/tab-3.png",
          "selectedIconPath": "/static/icon/tab-4.png",
          "isSpecial": true,
          "text": "智能对话"
        },
        {
          "pagePath": "/pages/PersonalCenter/PersonalCenter",
          "iconPath": "/static/icon/tab-5.png",
          "selectedIconPath": "/static/icon/tab-6.png",
          "text": "我的"
        }
      ]
    }
  }
})